package com.tamtamy.jtta.demo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

import com.tamtamy.jtta.JTTAClient;
import com.tamtamy.jtta.constant.Type;
import com.tamtamy.jtta.data.Content;
import com.tamtamy.jtta.data.Notice;
import com.tamtamy.jtta.data.UserTag;
import com.tamtamy.jtta.exception.UnauthorizedException;

public class JTTADemo {

	private BufferedReader stdin; 
	private boolean login = false;
	private JTTAClient tta;

	private String startOffset = null;
	private String limit = null;


	private JTTADemo(){
		stdin = new BufferedReader( new InputStreamReader( System.in ) );
		tta = null;
	}

	private void prompt() throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("\n\n");		
		sb.append("jTTA Menu - Choose an option:\n");
		if(!login){
			sb.append("\t (0) login\n");
		}else{
			sb.append("\t (0) logout\n");			
			sb.append("\t (1) tag list\n");
			sb.append("\t (2) tag details\n");
			sb.append("\t (3) contents list\n");
			sb.append("\t (4) content details\n");
			sb.append("\t (5) users list\n");
			sb.append("\t (6) on-line users\n");
			sb.append("\t (7) user details\n");
			sb.append("\t (8) content download\n");
			sb.append("\t (9) comments list\n");
			sb.append("\t(10) comments\n");
			sb.append("\t(11) notice list\n");
			sb.append("\t(12) notice\n");
			sb.append("\t(13) user stats\n");
			sb.append("\t(14) user stats generation\n");
			sb.append("\t(15) user stats generation details\n");
			sb.append("\t(16) group\n");
			sb.append("\t(17) group details\n");
			sb.append("\t(18) network\n");
			sb.append("\t(19) network details\n");
			sb.append("\t(20) metadata\n");
			sb.append("\t(21) user tag\n");
			sb.append("\n");
		}
		sb.append("\t (100) EXIT\n");
		sb.append("\n");
		sb.append("---> ");
		System.out.print(sb.toString());
		String input = stdin.readLine();
		if(input.equals("100"))
			choose(Integer.parseInt(input));
		else if((!login && !input.equals("0"))){
			System.out.println("Please Login with '0'");
			prompt();
		}
		choose(Integer.parseInt(input));

	}

	private void choose(int i){
		try{
			switch (i){
			case 0:
				auth();
				break;				
			case 1: 
				System.out.println(" - tag list ");
				getStarOffsetAndLimit();
				System.out.print(tta.getTagList(Long.parseLong(startOffset),Long.parseLong(limit)).toString());			
				break;
			case 2:
				System.out.print(" - tag: ");
				String tag = stdin.readLine();
				System.out.print(tta.getTagDetail(tag).toString());	
				break;
			case 3:
				System.out.println(" - contents list ");
				getStarOffsetAndLimit();
				System.out.println(" - available filters - ");

				ArrayList<String> filters = Content.getFilters();				
				for(int f=0; f<filters.size(); f++){
					System.out.println("\t" + f + " " + filters.get(f));
				}
				System.out.print(" - choose filter: ");
				String filter = stdin.readLine();
				filter = filters.get(Integer.parseInt(filter));

				ArrayList<String> orders = Content.getOrders();				
				for(int f=0; f<orders.size(); f++){
					System.out.println("\t" + f + " " + orders.get(f));
				}
				System.out.print(" - choose order: ");
				String order = stdin.readLine();
				order = orders.get(Integer.parseInt(order));

				tta.getContentList(Long.parseLong(startOffset),Long.parseLong(limit), filter, order).toString();
				break;
			case 4:
				System.out.println(" - content details: ");
				System.out.println("  - choose a content type: ");
				System.out.println("    a) File");
				System.out.println("    b) Link");
				System.out.println("    c) Message");
				System.out.println("    d) Widget");
				String contentType = stdin.readLine();
				System.out.print(" - content file ID: ");
				String contentFileID = stdin.readLine();
				if(contentType.equals("a"))
					System.out.print(tta.getContentFile(contentFileID).toString());
				else if(contentType.equals("b"))
					System.out.print(tta.getContentLink(contentFileID).toString());
				else if(contentType.equals("c"))
					System.out.print(tta.getContentMessage(contentFileID).toString());
				else if(contentType.equals("d"))	
					System.out.print(tta.getContentWidget(contentFileID).toString());
				else
					choose(4);	
				break;
			case 5:
				System.out.println(" - users list ");				
				getStarOffsetAndLimit();
				System.out.print(" - appling filter: ");
				filter = stdin.readLine();
				System.out.print(tta.getUsersList(Long.parseLong(startOffset), Long.parseLong(limit), filter).toString());
				break;
			case 6:
				System.out.println(" - on-line users list ");
				getStarOffsetAndLimit();
				System.out.print(tta.getOnLineUsersList(Long.parseLong(startOffset), Long.parseLong(limit)).toString());			
				break;
			case 7:
				System.out.print(" - userID: ");
				String userID = stdin.readLine();
				System.out.print(tta.getUserDetail(userID).toString());
				break;
			case 8:				
				System.out.print(" - Content Download");
				System.out.print(" - contentID: ");
				String contentID = stdin.readLine();
				tta.downloadContent(contentID);
				break;
			case 9:				
				System.out.println(" - Comment List");				
				getStarOffsetAndLimit();
				System.out.print(tta.getCommentList(Long.parseLong(startOffset), Long.parseLong(limit)).toString());
				break;
			case 10:				
				System.out.println(" - Comment");				
				System.out.print(" - Comment ID: ");
				String commentID = stdin.readLine();
				System.out.print(tta.getComment(Long.parseLong(commentID)).toString());
				break;
			case 11:				
				System.out.println(" - Notice List");				
				getStarOffsetAndLimit();

				ArrayList<String> noticeFilters = Notice.getFilters();				
				for(int f=0; f<noticeFilters.size(); f++){
					System.out.println("\t" + f + " " + noticeFilters.get(f));
				}
				System.out.print(" - choose filter: ");
				String noticeFilter = stdin.readLine();
				noticeFilter = noticeFilters.get(Integer.parseInt(noticeFilter));

				System.out.print(tta.getNoticeList(Long.parseLong(startOffset), Long.parseLong(limit), noticeFilter).toString());
				break;
			case 12:				
				System.out.println(" - Notice");				
				System.out.print(" - Notice ID:");
				String noticeID = stdin.readLine();
				System.out.print(tta.getNotice(Long.parseLong(noticeID)).toString());
				break;
			case 13:				
				System.out.println(" - User Stats");
				System.out.print(" - UserStats ID: ");
				String userStatsID = stdin.readLine();
				getStarOffsetAndLimit();
				System.out.print(tta.getUserStatsList(userStatsID, Long.parseLong(startOffset), Long.parseLong(limit)).toString());
				break;
			case 14:				
				System.out.println(" - User Stats Generation");
				getStarOffsetAndLimit();
				System.out.print(tta.getUserStatsGenerationList(Long.parseLong(startOffset), Long.parseLong(limit)).toString());
				break;				
			case 15:				
				System.out.println(" - User Stats Generation Details");
				System.out.print(" - User Stats Generation ID: ");
				String userStatsGenerationID = stdin.readLine();
				getStarOffsetAndLimit();
				System.out.print(tta.getUserStatsGenerationDetails(userStatsGenerationID, Long.parseLong(startOffset), Long.parseLong(limit)).toString());
				break;
			case 16:				
				System.out.println(" - Groups");
				getStarOffsetAndLimit();
				System.out.print(tta.getGroupList(Long.parseLong(startOffset), Long.parseLong(limit)).toString());
				break;	
			case 17:				
				System.out.println(" - Group Details");
				System.out.print(" - Group ID: ");
				String groupID = stdin.readLine();
				System.out.print(tta.getGroupDetails(groupID).toString());
				break;
			case 18:				
				System.out.println(" - Network");
				getStarOffsetAndLimit();				
				System.out.print(tta.getNetworkList(Long.parseLong(startOffset), Long.parseLong(limit)).toString());
				break;				
			case 19:				
				System.out.println(" - Network Details");
				System.out.print(" - Network ID: ");
				String networkID = stdin.readLine();		
				System.out.print(tta.getNetworkDetails(networkID).toString());
				break;
			case 20:				
				System.out.println(" - MEtadata");
				System.out.print(" - url: ");
				String url = stdin.readLine();		
				System.out.print(tta.getMetadata(url).toString());
				break;
			case 21:				
				System.out.println(" - User Tag");
				System.out.print(" - UserID: (null for self) ");
				String userTagID = stdin.readLine();
				ArrayList<String> userTagFilters = UserTag.getFilters();				
				for(int f=0; f<userTagFilters.size(); f++){
					System.out.println("\t" + f + " " + userTagFilters.get(f));
				}
				System.out.print(" - choose filter: ");
				String userTagFilter = stdin.readLine();
				if(userTagID.equals("null"))
					System.out.print(tta.getUserTag(userTagFilters.get(Integer.parseInt(userTagFilter))).toString());
				else
				break;
			case 100:
				if(login) tta.doLogout();
				System.out.print("Execution terminated");
				System.exit(1);			
			default:
				prompt();
			}			
			prompt();
		}catch(Exception ex){
			ex.printStackTrace();
			//String[] args = null;
			//main(args);
		}

	}

	private void getStarOffsetAndLimit() throws Exception{
		System.out.print("  - display from index (a for all): ");
		startOffset = stdin.readLine();
		if(!startOffset.equals("a")){
			System.out.print("  - items to display: ");			
			limit = stdin.readLine();
			//startOffset = "0";
		}else if(startOffset.equals("a")){
			startOffset = "0";
			limit = Integer.MAX_VALUE+"";
		}
	}

	public static void main(String[] args){
		JTTADemo jttac = new JTTADemo();
		try{
			jttac.prompt();
		}catch(Exception ex){
			main(args);
		}
	}

	private void auth() throws UnauthorizedException{
		if(!login){	
			try{
			tta = new JTTAClient();
			String username = tta.getUsername();
			String password = tta.getPassword();			

				if(username==null || username.length()==0){
					System.out.print("username: ");
					username = stdin.readLine();
				}
				if(password==null || password.length()==0){
					System.out.print("password: ");
					password = stdin.readLine();
				}				
				System.out.print("type: ");
				System.out.print(" 1. XML ");
				System.out.print(" 2. JSON ");
				String type = stdin.readLine();
				if(type.equals("1")){
					tta.setType(Type.XML);
				}else if(type.equals("2")){
					tta.setType(Type.JSON);
				}	
			
			tta.doLogin(username, password);
			if(tta.token!=null){
				login = true;
				System.out.println();
				System.out.println("LOGIN OK - using TamTamyAPI version " + tta.getVersion());					  
			}
			}catch(Exception exception){
				System.err.println("LOGIN ERROR " + exception.getMessage());
			}
		}else{
			try {
				tta.doLogout();
				System.exit(100);
				login = false;
				System.out.println("LOGOUT OK");
			} catch(Exception exception){
				System.err.println("LOGOUT ERROR " + exception.getMessage());
			}

		}
		System.out.println("->" + login + "<-");
	}

}